<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

function get_latest_news($model, $limit) {
    if (MEMCACHED_ENABLED == "true") {
        $cache = get_cache(INSTANCE_HOME_NEWS, LASTEST_NEWS);
        if ($cache) {
            $data = $cache;
        } else {
            $data = $model->get_latest_news(0, $limit);
            set_cache(INSTANCE_HOME_NEWS, LASTEST_NEWS, $data, EXPIRED_LASTEST_NEWS);
        }
        return $data;
    } else {
        return $model->get_latest_news(0, $limit);
    }
}

function get_features_news($model) {
    if (MEMCACHED_ENABLED == "true") {
        $cache = get_cache(INSTANCE_HOME_NEWS, FEATURE_NEWS);
        if ($cache) {
            $data = $cache;
        } else {
            $data = $model->get_feature_news(0, 16);
            set_cache(INSTANCE_HOME_NEWS, FEATURE_NEWS, $data, EXPIRED_FEATURE_NEWS);
        }
        return $data;
    } else {
        return $model->get_feature_news(0, 16);
    }
}

function get_home_faq($model) {
    if (MEMCACHED_ENABLED == "true") {
        $cache = get_cache(INSTANCE_HOME_NEWS, FAQ_HOME);
        if ($cache) {
            $data = $cache;
        } else {
            $data = $model->_get_faq_nopaging(0, 9);
            set_cache(INSTANCE_HOME_NEWS, FAQ_HOME, $data, EXPIRED_FAQ_HOME);
        }
        return $data;
    } else {
        return $model->_get_faq_nopaging(0, 9);
    }
}

function _get_list_video($model, $from_limit, $numberof, $titleSearch){
    return $model->_get_list_video($from_limit, $numberof, $titleSearch);
}

function _get_all_cate_customs($model){
    return $model->_get_all_cate_customs();
}

function get_home_adv($model) {
    if (MEMCACHED_ENABLED == "true") {
        $cache = get_cache(INSTANCE_HOME_NEWS, ADV_HOME);
        if ($cache) {
            $data = $cache;
        } else {
            $data = $model->_get_adv_nopaging(0, 20);
            set_cache(INSTANCE_HOME_NEWS, ADV_HOME, $data, EXPIRED_ADV_HOME);
        }
        return $data;
    } else {
        return $model->_get_adv_nopaging(0, 20);
    }
}

function get_cities_list($model) {
    if (MEMCACHED_ENABLED == "true") {
        $cache = get_cache(INSTANCE_HOME_NEWS, CITY);
        if ($cache) {
            $cities_list = $cache;
        } else {
            $cities_list = $model->get_cities_list();
            set_cache(INSTANCE_HOME_NEWS, CITY, $cities_list, EXPIRED_CITY);
        }
        return $cities_list;
    } else {
        return $model->get_cities_list();
    }
}

function get_newscate_list($news_cate_id, $model) {
    if (MEMCACHED_ENABLED == "true") {
        $cache = get_cache(INSTANCE_HOME_NEWS, NEWS_CATE);
        if ($cache) {
            $data = $cache;
        } else {
            $data = $model->_get_news_cate_list();
            set_cache(INSTANCE_HOME_NEWS, NEWS_CATE, $data, EXPIRED_NEWS_CATE);
        }
        $rows = array();
        foreach ($data as $key => $item) {
            if ($item->parent_id == $news_cate_id && $item->status == 1) {
                $rows[] = $item;
            }
        }
        return array($rows);
    } else {
        return $model->get_news_cate_nopaging($news_cate_id, 0, 100);
    }
}

function get_docscate_list($docs_cate_id, $model) {
    if (MEMCACHED_ENABLED == "true") {
        $cache = get_cache(INSTANCE_HOME_NEWS, NEWS_CATE);
        if ($cache) {
            $data = $cache;
        } else {
            $data = $model->_get_docs_cate_list();
            set_cache(INSTANCE_HOME_DOCS, DOCS_CATE, $data, EXPIRED_DOCS_CATE);
        }
        $rows = array();
        foreach ($data as $key => $item) {
            if ($item->parent_id == $docs_cate_id && $item->status == 1) {
                $rows[] = $item;
            }
        }
        return array($rows);
    } else {
        return $model->get_docs_cate_nopaging($docs_cate_id, 0, 100);
    }
}

function get_feed_gold($feed) {
    if (MEMCACHED_ENABLED == "true") {
        $cache = get_cache(INSTANCE_HOME_NEWS, FEED_GOLD);
        if ($cache) {
            $data = $cache;
        } else {
            $data = $feed->do_feed_gold();
            set_cache(INSTANCE_HOME_NEWS, FEED_GOLD, $data, EXPIRED_FEED_GOLD);
        }
        return $data;
    } else {
        return $feed->do_feed_gold();
    }
}

function get_feed_exchange($feed) {
    if (MEMCACHED_ENABLED == "true") {
        $cache = get_cache(INSTANCE_HOME_NEWS, FEED_EXCHANGE);
        if ($cache) {
            $data = $cache;
        } else {
            $data = $feed->do_feed_exchange_rate();
            set_cache(INSTANCE_HOME_NEWS, FEED_EXCHANGE, $data, EXPIRED_FEED_EXCHANGE);
        }
        return $data;
    } else {
        return $feed->do_feed_exchange_rate();
    }
}

function remove_cache_exchange() {
    remove_cache(INSTANCE_HOME_NEWS, FEED_EXCHANGE);
}

function get_feed_weather($file_loader) {
    if (MEMCACHED_ENABLED == "true") {
        $cache = get_cache(INSTANCE_HOME_NEWS, FEED_WEATHER);
        if ($cache) {
            $data = $cache;
        } else {
            $data = $file_loader->get_feed_weather_from_file();
            set_cache(INSTANCE_HOME_NEWS, FEED_WEATHER, $data, EXPIRED_FEED_WEATHER);
        }
        return $data;
    } else {
        return $file_loader->get_feed_weather_from_file();
    }
}

function remove_cache_weather() {
    remove_cache(INSTANCE_HOME_NEWS, FEED_WEATHER);
}

function get_company_paging($model, $start_index, $count) {
    if ($start_index < 0 || $count <= 0) {
        return array();
    }
    if (MEMCACHED_ENABLED == "true") {
        $cache = get_cache(INSTANCE_HOME_NEWS, COMPANY);
        if ($cache) {
            $data = $cache;
        } else {
            $data = $model->get_all_company();
            set_cache(INSTANCE_HOME_NEWS, COMPANY, $data, EXPIRED_COMPANY);
        }
        if ($count >= sizeof($data)) {
            $count = sizeof($data);
        }
        for ($i = $start_index; $i < $start_index + $count; $i++) {
            $arr[] = $data[$i];
        }
        return array(sizeof($data), $arr);
    } else {
        return $model->get_company_paging($start_index, $count);
    }
}

function get_news_desc_paging($model, $new_parent_id, $news_cate, $start_index, $count) {
    if ($start_index < 0 || $count <= 0) {
        return array();
    }
    if (MEMCACHED_ENABLED == "true") {
        switch ($new_parent_id) {
            case "":
                $key = NEWS_TIN_XNK;
                break;
            case NEWS_CATE_VBPQ:
                $key = NEWS_VBPQ;
                break;
            case NEWS_CATE_KTXNK:
                $key = NEWS_KTXNK;
                break;
            case NEWS_CATE_TTHQ:
                $key = NEWS_TTHQ;
                break;
            case NEWS_CATE_NVGN:
                $key = NEWS_NVGN;
                break;
            case NEWS_CATE_NVK:
                $key = NEWS_NVLQ;
                break;
        }
        $cache = get_cache(INSTANCE_NEWS, $key);
        if ($cache) {
            $data = $cache;
        } else {
            $data = $model->get_list_news_info($news_cate);
            set_cache(INSTANCE_NEWS, $key, $data, EXPIRED_NEWS_LIST);
        }
        if ($count >= sizeof($data)) {
            $count = sizeof($data);
        }
        for ($i = $start_index; $i < $start_index + $count; $i++) {
            $arr_info[] = $data[$i];
        }
        foreach ($arr_info as $info) {
            $arr_desc[] = get_desc($model, $info->news_id);
        }
        return array(sizeof($data), $arr_desc);
    } else {
        return $model->get_news_paging($news_cate, $start_index, $count);
    }
}

function get_docs_desc_paging($model, $docs_parent_id, $docs_cate, $start_index, $count) {
    if ($start_index < 0 || $count <= 0) {
        return array();
    }
    if (MEMCACHED_ENABLED == "true") {
        switch ($docs_parent_id) {
            case DOCS_CATE_VBPQ:
                $key = DOCS_VBPQ;
                break;
        }
        $cache = get_cache(INSTANCE_DOCS, $key);
        if ($cache) {
            $data = $cache;
        } else {
            $data = $model->get_list_docs_info($docs_cate);
            set_cache(INSTANCE_DOCS, $key, $data, EXPIRED_DOCS_LIST);
        }
        if ($count >= sizeof($data)) {
            $count = sizeof($data);
        }
        for ($i = $start_index; $i < $start_index + $count; $i++) {
            $arr_info[] = $data[$i];
        }
        foreach ($arr_info as $info) {
            $arr_desc[] = get_desc($model, $info->docs_id);
        }
        return array(sizeof($data), $arr_desc);
    } else {
        return $model->get_docs_paging($docs_cate, $start_index, $count);
    }
}

function get_desc($model, $id) {
    if (MEMCACHED_ENABLED == "true") {
        $key = $id . NEWS_DESC;
        $cache = get_cache(INSTANCE_NEWS, $key);
        if ($cache) {
            $data = $cache;
        } else {
            $data = $model->get_desc($id);
            set_cache(INSTANCE_NEWS, $key, $data, EXPIRED_NEWS_DESC);
        }
    } else {
        $data = $model->get_desc($id);
    }
    return $data[0];
}

function get_news_detail($model, $id) {
    if (MEMCACHED_ENABLED == "true") {
        $key = $id . NEWS_DETAIL;
        $cache = get_cache(INSTANCE_NEWS, $key);
        if ($cache) {
            $data = $cache;
        } else {
            $data = $model->get_news_detail($id);
            set_cache(INSTANCE_NEWS, $key, $data, EXPIRED_NEWS_DETAIL);
        }
    } else {
        $data = $model->get_news_detail($id);
    }
    return $data[0];
}

function get_features_news_for_detail($model, $cate_id) {
    $data = get_features_news($model);
    $result = array();
    foreach ($data[0] as $item) {
        if ($item->cate_id == $cate_id) {
            $result[] = $item;
        }
    }
    return array($result);
}

function get_most_view_news($model, $cate_id) {
    if (MEMCACHED_ENABLED == "true") {
        $key = $cate_id . NEWS_MOST_VIEW;
        $cache = get_cache(INSTANCE_NEWS, $key);
        if ($cache) {
            $data = $cache;
        } else {
            $data = $model->get_most_view_news($cate_id, 0, 20);
            set_cache(INSTANCE_NEWS, $key, $data, EXPIRED_MOST_VIEW_NEWS);
        }
    } else {
        $data = $model->get_most_view_news($cate_id, 0, 20);
    }
    return $data;
}

function get_others_news($model, $parent_id, $cate_id, $news_id) {
    $data_desc = get_news_desc_paging($model, $parent_id, $cate_id, 0, 10);
    $result = array();
    foreach ($data_desc[1] as $desc) {
        if ($desc->news_id != $news_id) {
            $result[] = $desc;
        }
    }
    return array($result);
}

function set_cache($instance, $key, $value, $expire) {
    if (!$expire) {
        $expire = 0; // no expire
    }
    connect_cache($instance)->set($key, base64_encode(serialize($value)), false, $expire);
}

function get_cache($instance, $key) {
    $cache = connect_cache($instance)->get($key);
    return unserialize(base64_decode($cache));
}

function remove_cache($instance, $key) {
    connect_cache($instance)->delete($key);
}

function connect_cache($instance) {
    $MEMCACHED_SERVERS = array(MEMCACHED_IP, //add more server ip here
    );
    if (class_exists('Memcache')) {
        $memcachedObj = new Memcache;
        foreach ($MEMCACHED_SERVERS as $server) {
            switch ($instance) {
                case INSTANCE_NEWS:
                    $memcachedObj->connect($server, MEMCACHED_NEWS_PORT);
                    break;
                case INSTANCE_FAQ:
                    $memcachedObj->connect($server, MEMCACHED_FAQ_PORT);
                    break;
                case INSTANCE_ADV:
                    $memcachedObj->connect($server, MEMCACHED_ADV_PORT);
                    break;
                case INSTANCE_HOME_NEWS:
                    $memcachedObj->connect($server, MEMCACHED_HOME_NEWS_PORT);
                    break;
                default:
                    $memcachedObj->connect($server); //addServer($server);
                    break;
            }
        }
    } else {
        return false;
    }
    return $memcachedObj;
}

function getContentIndex($idCol, $tableName, $model){
    $data = $model->getContentIndex($idCol, $tableName);
    return $data;
}

?>
